/*
The MIT License (MIT)
Copyright (c) 2013 Dalvik Boss dalvikboss@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package sidl;

public class Type {
	public String name;
	public boolean isArray = false;

	public String toJava() {
		return name + (isArray ? "[]" : "");
	}

	public Type(String name) {
		this.name = name.replaceAll("\\[", "").replaceAll("]", "")
				.replaceAll(" ", "");
		if (name.contains("[")) {
			isArray = true;
		}
	}

	public boolean isVoid() {
		return name.equals("void");
	}

	public static final String[] prims = new String[] { "boolean", "char",
			"byte", "short", "int", "long", "float", "double" };

	public String toC() {
		return getC(name) + (isArray ? "_array" : "");
	}

	public String asReturnTypeOfC() {
		return getC(name) + (isArray ? "_array" : "")
				+ ((isClazz() || isArray) ? "*" : "");
	}

	private String getC(String name) {
		if (name.equals("String")) {
			return "cstring";
		} else {
			return name;
		}

	}

	public String asReturnTypeOfObjc(Doc doc) {
		if (isArray) {
			return "NSArray*";
		} else if(name.equals("String")) {
			return "NSString*";
		} else if (isClazz()) {
			return doc.cPackageName() + "_" + name + "*";
		} else if (isPrim()) {
			return asObjcName(name);
		} else {
			return "void";
		}

	}

	public String asReturnTypeOfQt() {
		return getQt(name) + (isArray ? "_array" : "")
				+ ((isClazz() || isArray) ? "*" : "");
	}

	private String getQt(String name) {
		if (name.equals("String")) {
			return "QString";
		} else {
			return name;
		}
	}

	private String getObjc(String name) {
		if (name.equals("String")) {
			return "NSString";
		} else {
			return name;
		}

	}

	public boolean isPrim() {
		for (String p : prims) {
			if (p.equals(name)) {
				return true;
			}
		}
		return false;

	}

	public boolean isClazz() {
		return !isPrim() && !isVoid();
	}

	public String asByteBufferPutType() {

		return name.replaceFirst(name.substring(0, 1), name.substring(0, 1)
				.toUpperCase());
	}

	public static String asQtName(String name) {
		if (name.equals("char")) {
			return "mychar";
		} else if (name.equals("boolean")) {
			return "mybool";
		} else if (name.equals("long")) {
			return "mylong";
		} else {
			return name;
		}
	}

	public static String asObjcName(String name) {
		if (name.equals("char")) {
			return "sidl_char";
		} else if (name.equals("boolean")) {
			return "sidl_bool";
		} else if (name.equals("byte")) {
			return "sidl_byte";
		} else if (name.equals("long")) {
			return "sidl_long";
		} else {
			return name;
		}
	}

	public String asQtTypeInWrite() {
		return asQtName(name) + (isArray ? "_array" : "");
	}

	public String asSidlType() {
		return name + (isArray ? "[]" : "");
	}

}
